function showBreadcrumb(ruta) {
	var frame = top.frames['title'];
	var cels = frame.document.getElementsByTagName('td');
	cels[0].innerHTML = '<p CLASS=SUBT align=left><b>' + ruta + '</b></p>';
}

function loadProfiles(bActive) {
	/*
	if (bActive == '1') {
		$('#divLeftSwitch').html('<a href="javascript:void(0);" onclick="javascript:loadProfiles(\'0\');">Mostrar Perfiles Inactivos</a>');
		$('#divLeftMainTitle').html('Perfiles Activos:');
		$('#profileSearch').val('Buscar perfil activo..');
		$('#profileType').val('1');
	}
	else {
		$('#divLeftSwitch').html('<a href="javascript:void(0);" onclick="javascript:loadProfiles(\'1\');">Mostrar Perfiles Activos</a>');
		$('#divLeftMainTitle').html('Perfiles Inactivos:');
		$('#profileSearch').val('Buscar perfil inactivo..');
		$('#profileType').val('0');
	}
	*/
	$('#profileNameAdd').val('');
	$('#profileDescriptionAdd').val('');
	// reset center & right area
	$('#divCenterMain').css("visibility","hidden");
	$('#divRightMain').css("visibility","hidden");
	$('#divCenterMainTitle').html('');
	$('#divRightMainTitle').html('');
	$('#modActionWLSearch').val('');
	$('#modActionBLSearch').val('');
	$.ajax({
        url: base_url + "roles/ajax-search/acl-roles?format=json",
        dataType: "json",
        data: {active:bActive},
        type: "POST",
        async: false,
        success: function(data) {
        	console.debug(data);
            if (data.STATUS == 'true') {
            	if ($.trim(data.VALUE).length > 0)
            		$('#divLeftList').html($.trim(data.VALUE));
            	else
            		$('#divLeftList').html('No hay perfiles guardados.');
            } else {
            	alert(data.MESSAGE);
            }
        },
        error: function (e) {
        	console.debug(e);
        }
    });
}

function findProfile() {
	var searchTerm = $('#profileSearch').val();
	var bActive = $('#profileType').val();
	if ( $.trim(searchTerm).length > 0 ) {
		$.ajax({
	        url: base_url + "/roles/ajax-search/acl-roles",
	        dataType: "json",
	        data: { nombre_perfil:searchTerm,
					active:bActive },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	if ($.trim(data.VALUE).length > 0)
	            		$('#divLeftList').html($.trim(data.VALUE));
	            	else
	            		$('#divLeftList').html('No hay resultados.');
	            }
	            else {
	            	alert(data.MESSAGE);
	            }
	        }
	    });
	}
	else {
		loadProfiles(bActive);
		$('#profileSearch').val('');
		$('#profileSearch').focus();
	}
}

function archiveProfile(iProfileID) {
	if (confirm(String.fromCharCode(191) + "Esta seguro que desea desactivar el perfil seleccionado?")) {
		$.ajax({
	        url: "../bl_privilegios/ajaxCRUD-perfil.php",
	        dataType: "json",
	        data: { action:'AR',
					id_perfil:iProfileID },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	alert(data.MESSAGE);
	            	loadProfiles('1');
	            }
	            else
	            	alert(data.MESSAGE);
			}
	    });
	}
	else return false;
}

function activateProfile(iProfileID) {
	if (confirm(String.fromCharCode(191) + "Esta seguro que desea activar el perfil seleccionado?")) {
		$.ajax({
	        url: "../bl_privilegios/ajaxCRUD-perfil.php",
	        dataType: "json",
	        data: { action:'AC',
					id_perfil:iProfileID },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	alert(data.MESSAGE);
	            	loadProfiles('0');
	            } else 	alert(data.MESSAGE);
			}
	    });
	} else	return false;
}

function editProfileName(iProfileID,sProfileName,bActive) {
	//var newProfileName = prompt("Por favor, ingrese el nuevo nombre del perfil.",decodeURIComponent((sProfileName+'').replace(/\+/g,'%20')));
	var newProfileName = prompt("Por favor, ingrese el nuevo nombre del perfil.",unescape(sProfileName).replace(/\+/g," "));
	if ( (newProfileName != null) && ($.trim(newProfileName).length > 0) ) {
		$.ajax({
	        url: base_url + "roles/ajax-crud/role",
	        dataType: "json",
	        data: { action:'U',
					roles_id:iProfileID,
					role_name:newProfileName },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	alert(data.MESSAGE);
	            	if (bActive == '1')
	            		loadProfiles('1');
	            	else
	            		loadProfiles('0');
	            }
	            else
	            	alert(data.MESSAGE);
			}
	    });
	} else if (($.trim(newProfileName).length == 0) && (newProfileName != null)) {
		alert("El nombre ingresado no es v" + String.fromCharCode(225) + "lido.");
		editProfileName(iProfileID,sProfileName,bActive);
	} else return false;
}

function editProfileDescription(iProfileID,sProfileDescription,bActive) {
	//var newProfileDescription = prompt("Por favor, ingrese la nueva descripci" + String.fromCharCode(243) + "n del perfil.",decodeURIComponent((sProfileDescription+'').replace(/\+/g,'%20')));
	var newProfileDescription = prompt("Por favor, ingrese la nueva descripci" + String.fromCharCode(243) + "n del perfil.",unescape(sProfileDescription).replace(/\+/g," "));
	if ( (newProfileDescription != null) && ($.trim(newProfileDescription).length > 0) ) {
		$.ajax({
			url: base_url + "roles/ajax-crud/role",
	        dataType: "json",
	        data: { action:'U',
					roles_id:iProfileID,
					description:newProfileDescription },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	alert(data.MESSAGE);
	            	if (bActive == '1')
	            		loadProfiles('1');
	            	else
	            		loadProfiles('0');
	            } else 	alert(data.MESSAGE);
			}
	    });
	} else if (($.trim(newProfileDescription).length == 0) && (newProfileDescription != null)) {
		alert("La descripcion ingresada no es v" + String.fromCharCode(225) + "lida.");
		editProfileDescription(iProfileID,sProfileDescription,bActive);
	} else	return false;
}

function addProfile() {
	var newProfileName 		  = $('#profileNameAdd').val();
	var newProfileDescription = $('#profileDescriptionAdd').val();
	var bValidData      	  = false;
	if ( ($.trim(newProfileName).length > 0) && ($.trim(newProfileDescription).length > 0) )
		bValidData = true;
	
	if (bValidData) {
		$.ajax({
	        url: base_url + "roles/ajax-crud/role",
	        dataType: "json",
	        data: { action:'C',
					nombre_perfil:newProfileName,
					descripcion_perfil:newProfileDescription },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	alert(data.MESSAGE);
	            	$('#profileNameAdd').val('');
	            	$('#profileDescriptionAdd').val('');
	            	$('#profileNameAdd').focus();
	            	loadProfiles('1');
	            } else {
	            	alert(data.MESSAGE);
	            	$('#profileNameAdd').val('');
	            	$('#profileDescriptionAdd').val('');
	            	$('#profileNameAdd').focus();
	            }
	        }
	    });
	}
	else {
		alert("Por favor, ingrese datos v" + String.fromCharCode(225) + "lidos para el nuevo perfil.");
		$('#profileNameAdd').val('');
    	$('#profileDescriptionAdd').val('');
		$('#profileNameAdd').focus();
	}
}

function loadProfileList(sProfileName,iProfileID,bRelated) {
	// reset center & right area
	//$('#divCenterMainTitle').html('Acciones para ' + decodeURIComponent((sProfileName+'').replace(/\+/g,'%20')) + ':');
	$('#divCenterMainTitle').html('Acciones para ' + unescape(sProfileName).replace(/\+/g," ") + ':');
	$('#divRightMainTitle').html('Acciones NO Asociadas:');
	$('#divCenterMain').css("visibility","visible");
	$('#divRightMain').css("visibility","visible");
	$('#modActionWLSearch').val('Buscar accion..');
	$('#modActionBLSearch').val('Buscar accion..');
	// get data
	$('#profileID').val(iProfileID);
	$('#profileName').val(sProfileName);
	$.ajax({
        url: base_url + "roles/ajax-search/action-module-role",
        dataType: "json",
        data: { acl_roles_id:iProfileID,
				related:bRelated },
        type: "POST",
        async: false,
        success: function(data) {
            if (data.STATUS == 'true') {
            	if (bRelated) {
            		if ($.trim(data.VALUE).length > 0)
            			$('#divCenterList').html($.trim(data.VALUE));
            		else
            			$('#divCenterList').html('No hay resultados.');
            	}
            	else {
            		if ($.trim(data.VALUE).length > 0)
            			$('#divRightList').html($.trim(data.VALUE));
            		else
            			$('#divRightList').html('No hay resultados.');
            	}
            }
            else {
            	alert(data.MESSAGE);
            }
        }
    });    	
}

function findModAction(bRelated) {	
	if (bRelated)
		var searchTerm = $('#modActionWLSearch').val();
	else
		var searchTerm = $('#modActionBLSearch').val();
	var iProfileID   = $('#profileID').val();
	var sProfileName = $('#profileName').val();
	
	if ( $.trim(searchTerm).length > 0 ) {
		$.ajax({
	        url: base_url + "roles/ajax-search/action-module-role",
	        dataType: "json",
	        data: { acl_roles_id:iProfileID,
					accion:searchTerm,
					related:bRelated },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	if (bRelated) {
	            		if ($.trim(data.VALUE).length > 0)
	            			$('#divCenterList').html($.trim(data.VALUE));
	            		else
	            			$('#divCenterList').html('No hay resultados.');
	            	}
	            	else {
	            		if ($.trim(data.VALUE).length > 0)
	            			$('#divRightList').html($.trim(data.VALUE));
	            		else
	            			$('#divRightList').html('No hay resultados.');
	            	}
	            } else {
	            	alert(data.MESSAGE);
	            }
	        }
	    });
	} else {
		loadProfileList(sProfileName,iProfileID,bRelated);
		if (bRelated) {
			$('#modActionWLSearch').val('');
			$('#modActionWLSearch').focus();
		} else {
			$('#modActionBLSearch').val('');
			$('#modActionBLSearch').focus();
		}
	}
}

function addAction(iAccionModuloID, iProfileID, iPermission) {
	//if (confirm(String.fromCharCode(191) + "Esta seguro que desea agregar la acci" + String.fromCharCode(243) + "n seleccionada?")) {
		var sProfileName = $('#profileName').val();
		$.ajax({
	        url: base_url + "roles/ajax-crud/action-module-role",
	        dataType: "json",
	        data: { action:'C',
					modules_id:iAccionModuloID,
					roles_id:iProfileID, 
					permission:iPermission
				  },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	//alert(data.MESSAGE);
	            	loadProfileList(sProfileName,iProfileID,1);
	            	loadProfileList(sProfileName,iProfileID,0);
	            } else 	alert(data.MESSAGE);
			}
	    });
	//}
	//else
	//	return false;
}

function removeAction(iAccionModuloID,iProfileID,iPermission) {
	//if (confirm(String.fromCharCode(191) + "Esta seguro que desea quitar la acci" + String.fromCharCode(243) + "n seleccionada?")) {
		var sProfileName = $('#profileName').val();
		$.ajax({
			url: base_url + "roles/ajax-crud/action-module-role",
	        dataType: "json",
	        data: { action:'D',
					modules_id:iAccionModuloID,
					roles_id:iProfileID, 
					permission:iPermission
		  		  },
	        type: "POST",
	        async: false,
	        success: function(data) {
	            if (data.STATUS == 'true') {
	            	console.log(data.MESSAGE);
	            	loadProfileList(sProfileName,iProfileID,1);
	            	loadProfileList(sProfileName,iProfileID,0);
	            }
	            else
	            	alert(data.MESSAGE);
			}
	    });
	//}
	//else
	//	return false;
}